import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
import {
  Layout
  //getParentLayout
} from '@/utils/routerHelper'
import { useI18n } from '@/hooks/web/useI18n'

const { t } = useI18n()

export const constantRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard/analysis',
    name: 'Root',
    meta: {
      hidden: true
    }
  },
  {
    path: '/redirect',
    component: Layout,
    name: 'Redirect',
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'Redirect',
        component: () => import('@/views/Redirect/Redirect.vue'),
        meta: {}
      }
    ],
    meta: {
      hidden: true,
      noTagsView: true
    }
  },
  {
    path: '/login',
    component: () => import('@/views/Login/Login.vue'),
    name: 'Login',
    meta: {
      hidden: true,
      title: t('router.login'),
      noTagsView: true
    }
  },
  {
    path: '/login-sso',
    component: () => import('@/views/Login/Login-sso.vue'),
    name: 'Login-sso',
    meta: {
      hidden: true,
      title: '单点登录',
      noTagsView: true
    }
  },
  {
    path: '/404',
    component: () => import('@/views/Error/404.vue'),
    name: 'NoFind',
    meta: {
      hidden: true,
      title: '404',
      noTagsView: true
    }
  }
]

export const asyncRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/dashboard',
    component: '#',
    name: 'Dashboard',
    meta: {
      title: '首页',
      hidden: false,
      alwaysShow: true,
      noCache: false,
      breadcrumb: true,
      affix: false,
      noTagsView: false,
      canTo: false,
      icon: 'ant-design:dashboard-filled'
    },
    redirect: '/dashboard/analysis',
    children: [
      {
        path: 'analysis',
        component: 'views/Dashboard/Analysis',
        name: 'Analysis',
        meta: {
          title: '分析页',
          hidden: false,
          alwaysShow: false,
          noCache: true,
          breadcrumb: true,
          affix: false,
          noTagsView: false,
          canTo: false
        }
      },
      {
        path: 'workplace',
        component: 'views/Dashboard/Workplace',
        name: 'Workplace',
        meta: {
          title: '工作台',
          hidden: false,
          alwaysShow: false,
          noCache: true,
          breadcrumb: false,
          affix: false,
          noTagsView: false,
          canTo: false
        }
      }
    ]
  },
  {
    path: '/dept',
    component: '#',
    name: 'Dept',
    meta: {
      title: '部门管理',
      hidden: false,
      alwaysShow: false,
      noCache: false,
      breadcrumb: false,
      affix: false,
      noTagsView: false,
      canTo: false,
      icon: 'ri:organization-chart'
    },
    redirect: '/dept/deptList',
    children: [
      {
        path: 'deptList',
        component: 'views/Dept/Index',
        name: 'DeptList',
        meta: {
          title: '部门管理',
          hidden: false,
          alwaysShow: false,
          noCache: false,
          breadcrumb: false,
          affix: false,
          noTagsView: false,
          canTo: false,
          icon: 'eos-icons:organization'
        }
      }
    ]
  },
  {
    path: '/authority',
    component: '#',
    name: 'Authority',
    meta: {
      title: '权限管理',
      hidden: false,
      alwaysShow: true,
      noCache: false,
      breadcrumb: false,
      affix: false,
      noTagsView: false,
      canTo: false,
      icon: 'ep:set-up'
    },
    redirect: '/authority/menu',
    children: [
      {
        path: 'menu',
        component: 'views/Menu/Index',
        name: 'Menu',
        meta: {
          title: '菜单管理',
          hidden: false,
          alwaysShow: false,
          noCache: false,
          breadcrumb: false,
          affix: false,
          noTagsView: false,
          canTo: false,
          icon: 'ant-design:menu-fold-outlined'
        }
      },
      {
        path: 'function',
        component: 'views/Function/Index',
        name: 'Function',
        meta: {
          title: '功能权限',
          hidden: false,
          alwaysShow: false,
          noCache: false,
          breadcrumb: false,
          affix: false,
          noTagsView: false,
          canTo: false,
          icon: 'ri:function-line'
        }
      },
      {
        path: 'data',
        component: 'views/Data/Index',
        name: 'Data',
        meta: {
          title: '数据权限',
          hidden: false,
          alwaysShow: false,
          noCache: false,
          breadcrumb: false,
          affix: false,
          noTagsView: false,
          canTo: true,
          icon: 'bx:data'
        }
      }
    ]
  },
  {
    path: '/admin',
    component: '#',
    name: 'Admin',
    meta: {
      title: '管理员管理',
      hidden: false,
      alwaysShow: false,
      noCache: false,
      breadcrumb: true,
      affix: false,
      noTagsView: false,
      canTo: false,
      icon: 'ep:user'
    },
    redirect: '/admin/adminList',
    children: [
      {
        path: 'adminList',
        component: 'views/Admin/Index',
        name: 'AdminList',
        meta: {
          title: '管理员列表',
          hidden: false,
          alwaysShow: false,
          noCache: false,
          breadcrumb: false,
          affix: false,
          noTagsView: false,
          canTo: false,
          icon: 'ep:user'
        }
      }
    ]
  },
  {
    path: '/role',
    component: '#',
    name: 'Role',
    meta: {
      title: '角色管理',
      hidden: false,
      alwaysShow: false,
      noCache: false,
      breadcrumb: false,
      affix: false,
      noTagsView: false,
      canTo: false,
      icon: 'ep:avatar'
    },
    redirect: '/role/roleList',
    children: [
      {
        path: 'roleList',
        component: 'views/Role/Index',
        name: 'RoleList',
        meta: {
          title: '角色列表',
          hidden: false,
          alwaysShow: false,
          noCache: false,
          breadcrumb: false,
          affix: false,
          noTagsView: false,
          canTo: false,
          icon: 'ep:avatar'
        }
      }
    ]
  },
  {
    path: '/dict',
    component: '#',
    name: 'Dict',
    meta: {
      title: '字典管理',
      hidden: false,
      alwaysShow: false,
      noCache: false,
      breadcrumb: false,
      affix: false,
      noTagsView: false,
      canTo: false,
      icon: 'dashicons:book'
    },
    redirect: '/dict/dictData',
    children: [
      {
        path: 'dictData',
        component: 'views/Dict/DictData',
        name: 'DictData',
        meta: {
          title: '字典数据',
          hidden: false,
          alwaysShow: false,
          noCache: false,
          breadcrumb: false,
          affix: false,
          noTagsView: false,
          canTo: false,
          icon: 'bxs:data'
        }
      },
      {
        path: 'dictType',
        component: 'views/Dict/DictType',
        name: 'EnumType',
        meta: {
          title: '字典类型',
          hidden: false,
          alwaysShow: false,
          noCache: false,
          breadcrumb: false,
          affix: false,
          noTagsView: false,
          canTo: false,
          icon: 'codicon:type-hierarchy'
        }
      },
      {
        path: 'dictRels',
        component: 'views/Dict/DictRels',
        name: 'DictRels',
        meta: {
          title: '字典联动',
          hidden: false,
          alwaysShow: false,
          noCache: false,
          breadcrumb: false,
          affix: false,
          noTagsView: false,
          canTo: false,
          icon: 'mdi:relation-many-to-many'
        }
      }
    ]
  },
  {
    path: '/userCenter',
    component: '#',
    name: 'userCenter',
    meta: {
      title: '用户中心',
      hidden: false,
      alwaysShow: false,
      noCache: false,
      breadcrumb: false,
      affix: false,
      noTagsView: false,
      canTo: false,
      icon: 'gis:globe-users'
    },
    redirect: '/userCenter/ucFunction',
    children: [
      {
        path: 'ucFunction',
        component: 'views/UserCenter/Function/Index',
        name: 'ucFunction',
        meta: {
          title: '功能权限管理',
          hidden: false,
          alwaysShow: false,
          noCache: false,
          breadcrumb: false,
          affix: false,
          noTagsView: false,
          canTo: false,
          icon: 'ri:function-line'
        }
      },
      {
        path: 'ucSysRole',
        component: 'views/UserCenter/SysRole/Index',
        name: 'UcSysRole',
        meta: {
          title: '系统角色管理',
          hidden: false,
          alwaysShow: false,
          noCache: false,
          breadcrumb: false,
          affix: false,
          noTagsView: false,
          canTo: false,
          icon: 'eos-icons:role-binding'
        }
      },
      {
        path: 'org',
        component: 'views/UserCenter/Org/Index',
        name: 'Org',
        meta: {
          title: '组织管理',
          hidden: false,
          alwaysShow: false,
          noCache: false,
          breadcrumb: false,
          affix: false,
          noTagsView: false,
          canTo: false,
          icon: 'ps:organisation'
        }
      },
      {
        path: 'user',
        component: 'views/UserCenter/User/Index',
        name: 'User',
        meta: {
          title: '用户管理',
          hidden: false,
          alwaysShow: false,
          noCache: false,
          breadcrumb: false,
          affix: false,
          noTagsView: false,
          canTo: false,
          icon: 'ep:user-filled'
        }
      }
    ]
  },
  {
    path: '/cs',
    component: '#',
    name: 'CattleShop',
    meta: {
      title: '牛淘宝',
      hidden: false,
      alwaysShow: false,
      noCache: false,
      breadcrumb: false,
      affix: false,
      noTagsView: false,
      canTo: false,
      icon: 'icon-park-outline:cattle'
    },
    children: [
      {
        path: 'class',
        component: 'views/CattleShop/Class/Index',
        name: 'Class',
        meta: {
          title: '分类管理',
          hidden: false,
          alwaysShow: false,
          noCache: false,
          breadcrumb: false,
          affix: false,
          noTagsView: false,
          canTo: false,
          icon: 'codicon:symbol-class'
        }
      },
      {
        path: 'ClassTag',
        component: 'views/CattleShop/ClassTag/Index',
        name: 'ClassTag',
        meta: {
          title: '分类与标签关系',
          hidden: false,
          alwaysShow: false,
          noCache: false,
          breadcrumb: false,
          affix: false,
          noTagsView: false,
          canTo: false,
          icon: 'eos-icons:storage-class'
        }
      },
      {
        path: 'TagGroup',
        component: 'views/CattleShop/TagGroup/Index',
        name: 'TagGroup',
        meta: {
          title: '标签组',
          hidden: false,
          alwaysShow: false,
          noCache: false,
          breadcrumb: false,
          affix: false,
          noTagsView: false,
          canTo: false,
          icon: 'fluent:tag-multiple-16-filled'
        }
      },
      {
        path: 'Tab',
        component: 'views/CattleShop/Tag/Index',
        name: 'Tab',
        meta: {
          title: '标签管理',
          hidden: false,
          alwaysShow: false,
          noCache: false,
          breadcrumb: false,
          affix: false,
          noTagsView: false,
          canTo: false,
          icon: 'fluent:tag-16-filled'
        }
      },
      {
        path: 'goods',
        component: 'views/CattleShop/Goods/Index',
        name: 'Goods',
        meta: {
          title: '商品管理',
          hidden: false,
          alwaysShow: false,
          noCache: false,
          breadcrumb: false,
          affix: false,
          noTagsView: false,
          canTo: false,
          icon: 'ep:goods'
        }
      },
      {
        path: 'order',
        component: 'views/CattleShop/Order/Index',
        name: 'Order',
        meta: {
          title: '订单管理',
          hidden: false,
          alwaysShow: false,
          noCache: false,
          breadcrumb: false,
          affix: false,
          noTagsView: false,
          canTo: false,
          icon: 'ep:shopping-cart-full'
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  strict: true,
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 })
})

export const resetRouter = (): void => {
  const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !resetWhiteNameList.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export default router
